package cn.edu.scau.cmi.ema.controller.rest;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.io.IOException;

import com.google.gson.Gson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiPsonService;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;

import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;

@Controller("Event9zxlRestController")
public class Event9zxlRestController extends CmiEmaController {

	/**
	 * Create a new Event9zxl entity
	 * 
	 */
	@RequestMapping(value = "/newEvent9zxl", method = RequestMethod.POST)
	@ResponseBody
	public Object newEvent9zxl(@RequestBody Event9zxl event9zxl) {
		event9zxlService.saveEvent9zxl(event9zxl);
		return JSONObject.parse(cmiPsonService.toJsonString(event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxl.getId()), 2));
	}

	@RequestMapping(value = "/restNewEvent9zxl", method = RequestMethod.POST)
	@ResponseBody
	public Object restNewEvent9zxl(@RequestBody String event9zxlJson) {
		Event9zxl event9zxl = new Gson().fromJson(event9zxlJson, Event9zxl.class);
		appendReferences(event9zxl);
		event9zxlService.saveEvent9zxl(event9zxl);
		return JSONObject.parse(cmiPsonService.toJsonString(event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxl.getId()), 2));
	}

	@RequestMapping(value = "/saveEvent9zxl", method = RequestMethod.PUT)
	@ResponseBody
	public Event9zxl saveEvent9zxl(@RequestBody Event9zxl event9zxl) {
		event9zxlService.saveEvent9zxl(event9zxl);
		return event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxl.getId());
	}

	@RequestMapping(value = "/restSaveEvent9zxl", method = RequestMethod.PUT)
	@ResponseBody
	public Object restSaveEvent9zxl(@RequestBody String event9zxlJson) {
		Event9zxl event9zxl = new Gson().fromJson(event9zxlJson, Event9zxl.class);
		appendReferences(event9zxl);
		CmiPsonService cmiPsonService = new CmiPsonService();
		event9zxlService.saveEvent9zxl(event9zxl);
		return JSONObject.parse(cmiPsonService.toJsonString(event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxl.getId()), 1));
	}

	@RequestMapping(value = "/restDeleteEvent9zxl/{event9zxlId}", method = RequestMethod.DELETE)
	@ResponseBody
	public void restDeleteEvent9zxl(@PathVariable Integer event9zxlId) {
		Event9zxl event9zxl = event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxlId);
		event9zxlService.deleteEvent9zxl(event9zxl);
	}

	@RequestMapping(value = "/restConfirmDeleteEvent9zxls")
	@ResponseBody
	public Object restConfirmDeleteEvent9zxls(@RequestBody Set<Integer> deleteEvent9zxlsIds) {
		Set<Event9zxl> deleteEvent9zxls = event9zxlDAO.findEvent9zxlsByIds(deleteEvent9zxlsIds);
		Set<Event9zxl> referencedEvent9zxls = event9zxlService.loadReferenceEvent9zxls(deleteEvent9zxls);
		deleteEvent9zxls.removeAll(referencedEvent9zxls);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("referencedEntitys", referencedEvent9zxls);
		jsonObject.put("deleteEntitys", deleteEvent9zxls);
		return JSONObject.parse(jsonObject.toJSONString());
	}

	@RequestMapping("/restDeleteEvent9zxls")
	@ResponseBody
	public Object restDeleteEvent9zxls(@RequestBody Set<Event9zxl> deleteEvent9zxls) {
		Iterator<Event9zxl> iterator = deleteEvent9zxls.iterator();
		while (iterator.hasNext()) {
			Event9zxl currentEvent9zxl = iterator.next();
			event9zxlService.deleteEvent9zxl(currentEvent9zxl);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("url", "listEvent9zxls");
		return JSONObject.parse(jsonObject.toJSONString());
	}
	// loadself

	@RequestMapping(value = "/restLoadEvent9zxl/{event9zxlId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadEvent9zxl(@PathVariable Integer event9zxlId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject
				.parse(cmiPsonService.toPsonString(event9zxlDAO.findEvent9zxlByPrimaryKey(event9zxlId), 2, cmiPagination));
	}
	// loadReferenceds
	// TODO loadReferencedsPson，这个方法有一些问题，待修复

	public void appendReferences(Event9zxl event9zxl) {

	}

	@RequestMapping(value = "/restListEvent9zxls", method = RequestMethod.GET)
	@ResponseBody
	public Object restListEvent9zxls() {
		return JSONObject.parse(cmiPsonService.toJsonString(event9zxlService.loadEvent9zxls(), 2));
	}

	@RequestMapping(value = "/restListEvent9zxlsPost", method = RequestMethod.POST)
	@ResponseBody
	public Object restListEvent9zxlsPost(@RequestBody CmiPagination cmiPagination) {

		CmiPsonService cmiPsonService = new CmiPsonService();
		cmiPagination = event9zxlPaginationService.updateAppPagination(cmiPagination);
		int startResult = (cmiPagination.getCurrentPagination() - 1) * cmiPagination.getPaginationSize();

		Set<Event9zxl> event9zxls = event9zxlDAO.findAllEvent9zxls(startResult, cmiPagination.getPaginationSize());
		String event9zxlString = cmiPsonService.toPsonString(event9zxls, 1, cmiPagination);

		return JSONObject.parse(event9zxlString);
	}

	@RequestMapping("/event9zxlSelectBox")
	@ResponseBody
	public JSONObject event9zxlSelectBox(@RequestParam(required = false) String searchTable,
			@RequestParam(required = false) Set<Integer> searchValue, // 杨宏宇2019-9-10：searchValue是在selectpage.js的616行定义，如果要修改searchValue的名称，1.可以修改在selectpage.js修改；2.可以
																		// @RequestParam(value="searchValue", required =
																		// false) Set<Integer> someName
			@RequestParam(required = false, defaultValue = "10") int paginationSize,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(value = "qWord[]", required = false) String queryword,
			@RequestParam(required = false) String selectItem) throws IOException {
		Set entitySet = null;
		Integer count = 0;

		// 初始化原始值

		if (searchValue != null) {
			switch (searchTable) {
			// 外键

			// 被引用键
			}
			// 没有测试，深度需要多少？？？
			String jsonData = cmiPsonService.toJsonString(entitySet, 1);
			JSONArray jsonArray = new JSONArray();
			jsonArray = (JSONArray) JSON.parse((String) jsonData);

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("list", jsonArray);
			jsonObject.put("totalRow", entitySet.size());
			return jsonObject;
		}

		// 不根据关键字进行搜索
		if (queryword == null || queryword.trim() == "") {
			switch (selectItem) {
			// 外键

			// 被引用键
			}
		} else {
			switch (selectItem) {
			// 外键

			// 被引用键
			}
		}
		/**
		 * 将Json数据转换成jsonArray形式，需要通过pson方式进行转换，然后再转换成JsonArray。因为存在懒加载
		 * 没有直接使用CmiPsonService中的fromJson方法是因为，fromJson方法会将int类型的直接转换成double类型的数据，这个问题还没有解决
		 */
		// 没有测试，深度需要多少？？？
		String jsonData = cmiPsonService.toJsonString(entitySet, 1);
		JSONArray jsonArray = new JSONArray();
		jsonArray = (JSONArray) JSON.parse((String) jsonData);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("list", jsonArray);
		jsonObject.put("paginationSize", paginationSize);
		jsonObject.put("pageNumber", pageNumber);
		jsonObject.put("totalRow", count);
		jsonObject.put("totalPaginations",
				count % paginationSize == 0 ? count / paginationSize : count / paginationSize + 1);
		return jsonObject;
	}
}
